package box.data.util;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author <a mailto="wanglong635@126.com">Long</a>
 *
 */
public final class SingletonUtils {
        private static Map<String,Object> bean = new HashMap<String, Object>();

        @SuppressWarnings("unchecked")
        public static <T>T getSingleton(Class<T> clazz){
                Object result = bean.get(clazz.getName());
                if(result == null)
                        try {
                                Object obj = clazz.newInstance();
                                bean.put(clazz.getName(), obj);
                                return (T)obj;
                        } catch (InstantiationException e) {
                                e.printStackTrace();
                                throw new RuntimeException(e);
                        } catch (IllegalAccessException e) {
                                e.printStackTrace();
                                throw new RuntimeException(e);
                        }
                else
                        return (T)result;
        }

}
